<?php

namespace App\Http\AdminControllers;

use App\Service\Admin\AdminService;
use App\Service\AdmService;
use App\Support\JWT\MatrixToken;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use InvalidArgumentException;
use Laravel\Lumen\Http\ResponseFactory;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * @var AdmService
     */
    public $service;

    /**
     * 获取用户ID
     * @param bool $isForce 是否强制获取
     * @return string
     * @author LiWenYe
     */
    public function getUid($isForce = true) : string
    {
        /* @var $request Request */
        $request = app()->get('request');
        $token   = $request->header('w-access-token');
        if ( !$token) {
            throw new InvalidArgumentException(trans('auth.authorization is required'));
        }
        $explode = explode(' ', $token);
        if (isset($explode[1])) {
            $token = $explode[1];
        } elseif ($isForce) {
            throw new InvalidArgumentException(trans('auth.token is required'));
        } else {
            return '0';
        }

        if (empty($token)) {
            if ($isForce) {
                throw new InvalidArgumentException(trans('auth.token is required'));
            } else {
                return '0';
            }
        }
        $uid = (new MatrixToken())->getUidByJwt($token);

        if ( !$uid) {
            throw new InvalidArgumentException(trans('auth.token is invalid'));
        }

        return $uid;
    }

    // 是否超级管理员
    public function isSuperAdmin()
    {
        $adminService = new AdminService();

        $info = $adminService->getInfo($this->getUid());

        if ( !empty($info) && isset($info['data']['role_id']) && $info['data']['role_id'] == 1) {
            return true;
        }

        return false;
    }

    public function getTableInfo(Request $request)
    {
        $response = $this->service->getTableInfo();

        return response($response);
    }

    public function getInputs(Request $request)
    {
        $response = $this->service->getInputs();

        return response($response);
    }

    public function getInfo(Request $request)
    {
        $response = $this->service->getInfo($request->get('id', 0));

        return response($response);
    }

    /**
     * @desc 公共的新增或更新方法
     * @date 2020/7/15 2:45 下午
     * @param Request $request
     * @return Response|ResponseFactory
     * @throws ValidationException
     */
    public function modify(Request $request)
    {
        if ( !empty($this->service->getModel()->validate)) {
            $this->validate($request, $this->service->getModel()->validate);
        }

        $response = $this->service->modify($request->get('id', 0), $request->input());

        return response($response);
    }

    /**
     * @desc 公共的列表
     * @date 2020/7/15 2:45 下午
     * @param Request $request
     * @return Response|ResponseFactory
     */
    public function getList(Request $request)
    {
        $response = $this->service->getPageList(
            $request->get('page', 1),
            $request->get('limit', 20),
            $request->all()
        );

        return response($response);
    }

    public function delete(Request $request)
    {
        $response = $this->service->delete($request->get('id'));

        return response($response);
    }

}
